{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mean [WIP]\n",
    "\n",
    "Mean also referred to as the arithmetic mean is a measure of the central tendency of a set of numbers. You can calculate the mean of a set of numbers as follows:\n",
    "\n",
    "\n",
    "$$\n",
    "m=\\frac{\\text { sum of the terms }}{\\text { number of terms }}\n",
    "$$\n",
    "\n",
    "or\n",
    "\n",
    "more formally:\n",
    "\n",
    "$$\n",
    "\\bar{x}=\\frac{1}{n}\\left(\\sum_{i=1}^{n} x_{i}\\right)=\\frac{x_{1}+x_{2}+\\cdots+x_{n}}{n}\n",
    "$$\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.7317, -1.0386, -0.4281],\n",
      "        [ 1.1282, -0.7312,  0.2137],\n",
      "        [-1.4916, -0.6921,  0.7739],\n",
      "        [ 1.9643,  0.3754,  0.8449],\n",
      "        [ 1.0241, -0.4198, -0.2965]])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "tensor([[-0.2450],\n",
       "        [ 0.2036],\n",
       "        [-0.4699],\n",
       "        [ 1.0615],\n",
       "        [ 0.1026]])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "x = torch.randn(5, 3)\n",
    "print(x)\n",
    "\n",
    "# use keepdim=True to preserve dimension\n",
    "x.mean(-1, keepdim=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(42.)\n",
      "tensor(42.)\n"
     ]
    }
   ],
   "source": [
    "# Example 2:\n",
    "import torch\n",
    "\n",
    "x = torch.Tensor([4, 36, 45, 50, 75])\n",
    "\n",
    "print(torch.sum(x) / 5) # using sum and manual division\n",
    "print(torch.mean(x))    # using mean function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### References\n",
    "\n",
    "- [Wikipedia](https://en.wikipedia.org/wiki/Mean)\n",
    "- [Basics of Statistics for ML Engineer](https://medium.com/technology-nineleaps/basics-of-statistics-for-machine-learning-engineers-bf2887ac716c)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "d4d1e4263499bec80672ea0156c357c1ee493ec2b1c70f0acce89fc37c4a6abe"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
